import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


//准备actions-用于响应组件中的动作
const actions = {
    chooseit(context, value) {
        context.commit('Chooseit', value)
    }
}
//准备mutations-用于操作数据（state）
const mutations = {
    Chooseit(state, value) {
        state.select = value
        console.log('调用成功')
    }
}


export default new Vuex.Store({
    actions,
    mutations,
    state: {
        select: 1,
        dialog: 0,
        userAdmin: {},
        trendAdmin: {},
        activeAdmin: {},
        trendImg: [],
        appealAdmin: {},
        workAdmin: {},
        banId: 0,
        bodyId: 0,
        token: ''  //初始化token
    },
    // plugins: [persistedState()], //添加插件   保持vuex的数据持久化
    mutations: {
        //存储token方法
        //设置token等于外部传递进来的值
        setToken(state, token) {
            state.token = token
            localStorage.token = token //同步存储token至localStorage
            // sessionStorage.token = token //同步存储token至sessionStorage
        },
    },
    getters: {
        //获取token方法
        //判断是否有token,如果没有重新赋值，返回给state的token
        getToken(state) {
            if (!state.token) {
                state.token = localStorage.getItem('token')
            }
            return state.token
        }
    },
    actions: {

    }
})